package com.qf.springboot.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    //类级别的
    private static ApplicationContext ctx;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ctx = applicationContext;
    }

    public static Object getBean(String beanname){
        return ctx.getBean(beanname);
    }

    /**
     * 方法重载,根据传入的 class对象获取IOC容器中的bean
     * */
    public static <T> T getbean(Class<T> clazz){
        T bean = ctx.getBean(clazz);
        return bean;
    }
}
